package com.taichuan.selfcheck.mvp.presenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import com.orhanobut.logger.Logger;
import com.taichuan.mvp.base.MvpBasePresenter;
import com.taichuan.selfcheck.entity.SdCardInfo;
import com.taichuan.selfcheck.mvp.viewinterface.TestSdCardAndUsbInterface;
import com.taichuan.selfcheck.utils.SdCardInfoUtils;
import com.taichuan.tcutils.common.SDCardUtils;
import java.util.List;

/**
 * Created by CamelLuo on 2018/6/21.
 */
public class TestSdCardAndUsbPresenter extends MvpBasePresenter<TestSdCardAndUsbInterface> {
    private static final String TAG = TestSdCardAndUsbPresenter.class.getSimpleName();

    private SDCardReceiver sdCardReceiver;
    private Context context;

    public TestSdCardAndUsbPresenter(Context context) {
        this.context = context;
    }


    @Override
    public void attachView(TestSdCardAndUsbInterface view) {
        super.attachView(view);
        registerSdCardReceiver();
    }

    private void registerSdCardReceiver() {
        //注册广播接收者
        sdCardReceiver = new SDCardReceiver();
        IntentFilter intentFilter = new IntentFilter();
        // 2. 设置接收广播的类型
        intentFilter.addAction("android.os.storage.action.VOLUME_STATE_CHANGED");
        intentFilter.addAction("android.os.storage.extra.VOLUME_ID");
        intentFilter.addAction("android.os.storage.extra.VOLUME_STATE");
        intentFilter.addAction("android.intent.action.MEDIA_MOUNTED");
        intentFilter.addAction("android.intent.action.MEDIA_UNMOUNTED");
        intentFilter.addDataScheme("file");
        // 3. 动态注册：调用Context的registerReceiver（）方法
        context.registerReceiver(sdCardReceiver, intentFilter);
    }

    @Override
    public void detachView() {
        super.detachView();
        if (sdCardReceiver != null) {
            context.unregisterReceiver(sdCardReceiver);
        }
    }

    public void checkAndRefreshSdCardState() {
        //检查当前是否已经插入了外置SD卡
        if (SDCardUtils.hasSecondaryStorageSDCard(context)) {           //已插入
            //检测SD卡路径是否能够获取，能够获取就点亮图标
            getView().refreshSdCardState(true);
            //获取SD卡信息
            List<SdCardInfo> sdCardInfoList = SdCardInfoUtils.getSdCardInfo(context);
            //目前太川的设备只有一个SD卡槽，因此直接取第一个作为结果
            if (sdCardInfoList != null && sdCardInfoList.size() > 0) {
                getView().refreshSdCardInfo(sdCardInfoList.get(0));
            }
        } else {                                                         //未插入
            //检测SD卡路径是否能够获取，能够获取就点亮图标
            getView().refreshSdCardState(false);
        }
    }

    public class SDCardReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // 区分接收到的是哪个广播
            String action = intent.getAction();

            if (action != null) {
                Logger.t(TAG).e("==============接收到了广播，action为：  " + intent.getAction());
                /**
                 * 定义一个内部类广播接收者用于接收SD卡插入拔出状态
                 * SD卡插拔状态
                 * STATE_UNMOUNTED = 0;
                 * STATE_CHECKING = 1;
                 * STATE_MOUNTED = 2;
                 * STATE_MOUNTED_READ_ONLY = 3;
                 * STATE_FORMATTING = 4;
                 * STATE_EJECTING = 5;
                 * STATE_UNMOUNTABLE = 6;
                 * STATE_REMOVED = 7;
                 * STATE_BAD_REMOVAL = 8;
                 * 当SD卡插入卡槽后： 会由 STATE_UNMOUNTED -> STATE_MOUNTED 0 -> 2
                 * 当SD卡没有卸载直接强行拔出卡槽后： 会由 STATE_EJECTING -> STATE_UNMOUNTED -> STATE_BAD_REMOVAL 5 -> 0 -> 8
                 * 当SD卡卸载后再拔出卡槽后： 会由 STATE_EJECTING -> STATE_UNMOUNTED -> STATE_REMOVED  （没测试过，估计没有）
                 */
                if (action.equals("android.intent.action.MEDIA_MOUNTED")) {
                    Log.e(TAG, "onReceive: SD卡已挂载");



                    //检测SD卡路径是否能够获取，能够获取就点亮图标
                    getView().refreshSdCardState(true);
                    //获取SD卡信息
                    List<SdCardInfo> sdCardInfoList = SdCardInfoUtils.getSdCardInfo(context);
                    //目前太川的设备只有一个SD卡槽，因此直接取第一个作为结果
                    if (sdCardInfoList != null && sdCardInfoList.size() > 0) {
                        getView().refreshSdCardInfo(sdCardInfoList.get(0));
                    }

                } else if (action.equals("android.intent.action.MEDIA_UNMOUNTED")) {
                    //检测SD卡路径是否能够获取，能够获取就点亮图标
                    getView().refreshSdCardState(false);
                }
            }

        }
    }


}
